package com.p2p.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.p2p.model.entity.Bidrequest;
import com.p2p.model.entity.Bidrequestaudithistory;
import com.p2p.model.entity.Userinfo;
import com.p2p.model.vo.RealauthQueryVo;
import com.p2p.system.common.Result;
import com.p2p.model.entity.Realauth;
import com.p2p.system.exception.MyCustomerException;
import com.p2p.system.service.RealauthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 黄旭彬
 * @since 2023-12-13
 */

@CrossOrigin
@Api(tags = "信息认证控制器")
@RestController
@RequestMapping("/system/realauth")
public class RealauthController {

    @Autowired
    private RealauthService realauth;

    @ApiOperation("分页查询和条件查询")
    @GetMapping("/{page}/{limit}")
    public Result getRealauthList(
            @PathVariable Long page,
            @PathVariable Long limit,
            RealauthQueryVo realauthQueryVo
    ){

        Page<Realauth> pageParam = new Page<>(page,limit);
        IPage<Realauth> page1 =  realauth.selectRealauthList(pageParam,realauthQueryVo);
        return Result.ok(page1);
    }


    @ApiOperation("上传认证信息")
    @PostMapping("/attestation")
    public Result RealAuth(@RequestBody Realauth realauth){

        realauth.setAuditorId(1L);
        // 设置时间为当前时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        // 将当前时间格式化为字符串
        String formattedDateTime = currentDateTime.format(formatter);
        realauth.setApplytime(formattedDateTime);

        boolean save = this.realauth.save(realauth);
        if (save){
            return Result.ok();
        }
        throw new MyCustomerException(219, "上传失败，请检查认证信息是否正确");
    }


    @ApiOperation("根据id查找用户贷款申请表")
    @PostMapping("findrealById/{id}")
    public Result findUserById(@PathVariable Long id){
        Realauth byId = this.realauth.getById(id);
        if (byId!=null){
            return Result.ok(byId);
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("修改信息")
    @PostMapping("/update")
    public Result update(@RequestBody Realauth realauth){

        if (realauth.getSex().equals("男")){
            realauth.setSex(1);
        }else{
            realauth.setSex(0);
        }



        int update = this.realauth.updateByid(realauth);

        if (update>0) {
            return Result.ok().code(200).message("修改成功");
        }

        return Result.ok().code(201).message("修改失败");
    }

    @ApiOperation("审核实名信息")
    @PostMapping("/examine/{id}")
    public Result examine(@PathVariable Long id){

//        this.realauth.updateByid()

        return Result.ok();
    }

    @ApiOperation("审核通过")
    @PostMapping("ApprovedById")
    public Result ApprovedById1(@RequestBody Realauth realauth){
        realauth.setState(1);
        // 设置时间为当前时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        // 将当前时间格式化为字符串
        String formattedDateTime = currentDateTime.format(formatter);
        realauth.setAudittime(formattedDateTime);
        boolean save = this.realauth.updateById(realauth);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

}

